# build in python3.6.5
# author : hashaki
# Logistic regression Tutorial:https://machinelearningmastery.com/implement-logistic-regression-stochastic-gradient-descent-scratch-python/
import math

def predict(row,coefficients):
    yhat=coefficients[0]
    for i in range(len(row)-1):
        yhat+=coefficients[i+1]*row[i]
    return 1.0/(1.0+math.exp(-yhat))

# Using stochastic gradient descent to estimate
def coefficients_sgd(train,L_rate,n_epoch):
    coef=[0.0 for i in range(len(train[0]))] # why not using(0.0 for i in range(len(train[0])))?
    for epoch in range(n_epoch):
        sum_error=0
        for row in train:
            yhat=predict(row,coef)
            error=row[-1]-yhat
            sum_error+=error**2
            coef[0]=coef[0]+L_rate*error*yhat*(1.0-yhat)
            for i in range(len(row)-1):
                coef[i+1]=coef[i+1]+L_rate*error*yhat(1.0-yhat)*row
        print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, L_rate, sum_error))
        return coef

